﻿/////////////////////////////////////////////////////////////////////
//
//	Map_FVG C# Class Library.
//
//	MyControlTesto
//	
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////
using System;
using System.Drawing;
using System.Windows.Forms;


namespace Map_Fvg
{

    public partial class MyControlTesto : UserControl, IDragDropEnabled
    {
        private string Stx;
        private Color colorTextColor;
        private Font MyFont;
        private float angoloT;

        public MyControlTesto()
        {
            InitializeComponent();
            MyFont = this.Font;
            colorTextColor = Color.Black;
        }
        //         private void MyControlTesto_Paint(object sender, PaintEventArgs e)
        protected override void OnPaint(PaintEventArgs e)
        {
            if (!this.IsDragging)
            {
                if (ViewQ.Select_flag == (int)ViewQ.flagSelez.testo)
                {
                    Graphics dc = e.Graphics;

                    Color icolor = colorTextColor;
                    SolidBrush iBrush = new SolidBrush(icolor);
                    Font iFont = MyFont;
                    Pen blackPen = new Pen(Color.Red, 1);
                    bool bAngle = false;
  
                    float width1 = dc.MeasureString(Stx, iFont).Width + 10;
                    float height1 = dc.MeasureString(Stx, iFont).Height;
                    height1 = width1;

                    dc.RotateTransform(ViewQ.alpha);

                    Point[] polygon = new Point[5];
                    polygon[0].X = 1;
                    polygon[0].Y = 1;
                    polygon[1].X = ClientSize.Width - 2;
                    polygon[1].Y = 1;
                    polygon[2].X = ClientSize.Width - 2;
                    polygon[2].Y = ClientSize.Height - 2;
                    polygon[3].X = 1;
                    polygon[3].Y = ClientSize.Height - 2;
                    polygon[4] = polygon[0];
                    dc.DrawLines(blackPen, polygon);

                    Rectangle RectSelect = new Rectangle(0, 0, 3, 3);
                    RectSelect.X = polygon[0].X;
                    RectSelect.Y = polygon[0].Y; // +RectSelect.Height;
                    dc.DrawRectangle(blackPen, RectSelect);
                    SolidBrush blackBrush = new SolidBrush(Color.Black);
                    dc.FillRectangle(blackBrush, RectSelect);

                    RectSelect.X = polygon[1].X - RectSelect.Width;
                    RectSelect.Y = polygon[1].Y;
                    dc.DrawRectangle(blackPen, RectSelect);
                    dc.FillRectangle(blackBrush, RectSelect);

                    RectSelect.X = polygon[2].X - RectSelect.Width;
                    RectSelect.Y = polygon[2].Y - RectSelect.Height;
                    dc.DrawRectangle(blackPen, RectSelect);
                    dc.FillRectangle(blackBrush, RectSelect);

                    RectSelect.X = polygon[3].X;
                    RectSelect.Y = polygon[3].Y - RectSelect.Height;
                    dc.DrawRectangle(blackPen, RectSelect);
                    dc.FillRectangle(blackBrush, RectSelect);

                    width1 = dc.MeasureString(Stx, iFont).Width + 10;
                    height1 = dc.MeasureString(Stx, iFont).Height;

                    if (angoloT != 0.0F)
                    {
                        bAngle = true;
                    }

                    Point Pnt = new Point(0, 0);

                    SizeF stringSize = new SizeF();
                    stringSize = dc.MeasureString(Stx, iFont);

                    if (bAngle)
                    {
                        RectangleF drawRect = new RectangleF(0, 0, width1, height1);
                        Rectangle pntR = new Rectangle(0, 0, 2, 2);
                        dc.TranslateTransform(Pnt.X, Pnt.Y);
                        dc.RotateTransform(angoloT);


                        dc.DrawEllipse(blackPen, pntR);
                        dc.DrawString(Stx, iFont, iBrush, drawRect);
                        dc.ResetTransform();
                    }
                    else
                    {
                        RectangleF drawRect = new RectangleF(Pnt.X, Pnt.Y, width1, height1);
                        Rectangle pntR = new Rectangle(Pnt.X, Pnt.Y, 2, 2);
                        dc.DrawEllipse(blackPen, pntR);
                        dc.DrawString(Stx, iFont, iBrush, drawRect);


                        int height = this.ClientSize.Height / 2 - (int)(height1 / 2.0F);
                        Rectangle drawRect1 = new Rectangle(0, height, (int)width1, (int)height1);

                        dc.DrawString(Stx, iFont, iBrush, drawRect1);

                    } 
                    dc.ResetTransform();
                }
                
                         base.OnPaint(e);
            }
        }
        public float angolo
        {
            get
            {
                return angoloT;
            }
            set
            {
                angoloT = value;
            }
        }

        public string testo
        {
            get
            {
                return Stx;
            }
            set
            {
                if (value == null) return;
                Stx = string.Copy(value);

            }
        }

        public Color colore
        {
            get
            {
                return colorTextColor;
            }
            set
            {
                if (value != null) colorTextColor = value;
            }
        }
        public Font myFont
        {
            get
            {
                return MyFont;
            }
            set
            {
                if (value != null) MyFont = value;
            }
        }



        bool m_IsDragging = false;

        /// <summary>
        /// Required designer variable.
        /// </summary>
        //	private System.ComponentModel.Container components = null;

        /// <summary>
        /// Gets / Sets if the control is being dragged
        /// </summary>
        public bool IsDragging
        {
            get { return m_IsDragging; }
            set { m_IsDragging = value; }
        }

        /// <summary>
        /// Unused but in the interface
        /// </summary>
        public void StoreLocation()
        {


        }

        /// <summary>
        /// Unused but in the interface
        /// </summary>
        /// <param name="_successfull">unused</param>
        public void DropComplete(bool _successfull)
        {

        }

        public void completedDrag(int X, int Y)
        {
            Point Hit = new Point();
            Point Hit1 = new Point();

            double angleRad = -ViewQ.alpha * Math.PI / 180.0D; // 0F;

            Hit1.X = (int)((double)X * Math.Cos(angleRad) - (double)Y * Math.Sin(angleRad)) - ViewQ.Xoff;
            Hit1.Y = (int)((double)X * Math.Sin(angleRad) + (double)Y * Math.Cos(angleRad)) - ViewQ.Yoff;

            Hit.X = (int)(Hit1.X / ViewQ.scalaX) + ViewQ.Xmin;
            Hit.Y = (int)(Hit1.Y / ViewQ.scalaY) + ViewQ.Ymin;

            ViewQ.vectShapesT[ViewQ.i_selected].XX.X = Hit.X;
            ViewQ.vectShapesT[ViewQ.i_selected].XX.Y = Hit.Y;
            ViewQ.vectShapesT[ViewQ.i_selected].modifica = 1;

          
        }


    }
}
